package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactModificationTests extends TestBase{

	@Test (dataProvider = "randomValidContactGenerator")
	public void modifySomeContact(ContactData contact){
		
		//save old contacts list
		SortedListOf<ContactData> oldContacts = app.getContactHelper().getContacts();
				
		//modify contact
		Random rnd = new Random();
		int index = rnd.nextInt(oldContacts.size()-1);
		
		app.getContactHelper().modifyContact(index, contact);
				
		//save new contacts list
		SortedListOf<ContactData> newContacts = app.getContactHelper().getContacts();
		
		//compare lists
		contact.withContactId(oldContacts.get(index).getId());
		if (contact.getEmail() == "")
			contact.withEmail(contact.getAdditionalEmail());
		if (contact.getHomePhone() == "")
			if (contact.getMobilePhone() == "")
				contact.withHomePhone(contact.getWorkPhone());
			else
				contact.withHomePhone(contact.getMobilePhone());
		assertThat(newContacts, equalTo(oldContacts.without(index).withAdded(contact)));
	}
}
